package com.yaolaizaixian.merchant.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.base.BaseModel;
import com.yaolaizaixian.merchant.utils.GsonUtil;
import com.yaolaizaixian.merchant.utils.MyNetUtils;
import com.yaolaizaixian.merchant.utils.Toasty;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/5/15.
 */
public class InputQuanyiActivity extends BaseActivity {

    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.bt_commit)
    Button btCommit;


    private  String code="";

    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                //权益详情
                case 1:

                    String result5 = (String) msg.obj;
                    Log.e(TAG, "权益详情" + result5);
                    BaseModel baseModel5 = GsonUtil.jsonToBean(result5, BaseModel.class);
                    if (baseModel5.isFlag())
                    {
                        Intent data=new Intent();
                        data.putExtra("code", code);
                        setResult(109, data);
                        finish();
                    }
                    else
                    {
                        Toasty.error(getApplicationContext(), baseModel5.getErrorMsg(), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    break;
            }
        }
    };

    @Override
    public void initView() {


        setContentView(R.layout.activity_input);


    }

    @Override
    public void initData() {

        tvTitleText.setText("输入权益码");


    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.ll_back, R.id.bt_commit})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ll_back:

                finish();

                break;

            case R.id.bt_commit:



                if (!TextUtils.isEmpty(etNumber.getText().toString()))
                {

                    code=etNumber.getText().toString();
                    getQuanyiInfo(code);


                }
                else
                {
                    Toasty.error(getApplicationContext(),"请输入订单号").show();
                }

                break;
        }
    }

    //获取权益详情
    public void getQuanyiInfo(String code) {

        //店铺id
        final String url = BaseInfo.URL + "equityController/equityRuleInfoByCode.do";
        HashMap<String, String> sortedParams = new HashMap<String, String>();

        sortedParams.put("code", code);
        //sortedParams.put("customerSysno", customerId);
        sortedParams.put("customerSysno", "32808");
        OkHttpUtils
                .post()
                .url(url)
                .params((HashMap) MyNetUtils.getInstance().getNetData(sortedParams, getApplicationContext()))
                .build()
                .execute(new MyStringCallback());
    }

    class MyStringCallback extends StringCallback {
        @Override
        public void onError(Call call, Exception e, int id) {

        }

        @Override
        public void onResponse(String response, int id) {

            Message message = new Message();
            message.what = 1;
            message.obj = response;
            mhandler.sendMessage(message);
        }

    }

}
